function flashFormField(id, color, color2) {
	if (!color) {
		var color = '#d0d0d0';
	}
	if (!color2) {
		var color2 = '';
	}
	
	$(id).style.backgroundColor=color;
	setTimeout("$('"+id+"').style.backgroundColor='"+color2+"'", 300);
	setTimeout("$('"+id+"').style.backgroundColor='"+color+"'", 600);
	setTimeout("$('"+id+"').style.backgroundColor='"+color2+"'", 900);
	setTimeout("$('"+id+"').style.backgroundColor=''", 1200);
}

/* Web Service Helper */
function callWebService(service, method, params, callback) {
	try {
		url = geneurl + 'json.php?service='+service+'&method='+method;
		new ajax(url, {method: 'post', postBody: params, onComplete: callback});
	} catch (e) {
		alert(e);
	}
}

/* Search Box Enhancements */
var SiteSearch = {
	addSearch: function() {
		Event.observe('search', 'submit', SiteSearch.checkSearch);
	},
	
	checkSearch: function(event) {
		if ($('searchfield').value) {
			return true;
		} else {
			Event.stop(event);
			
			SiteSearch.alertError();
			$('searchfield').focus();
			
			return false;
		}
	},
	
	alertError: function() {
		flashFormField('searchfield');
	}
}

Event.observe(window, 'load', SiteSearch.addSearch, false);

/* Form Enhancements */
var enhancedEditor = false;
var skipCheck = false;

var EnhancedForms = {
	addCheck: function() {
		var forms = $('content').getElementsByTagName('form');
		for (i=0;i<forms.length;i++) {
			Event.observe(forms[i], 'submit', EnhancedForms.checkForm);
		}
		
		var skip = document.getElementsByClassName('skipcheck');
		for (i=0;i<skip.length;i++) {
			Event.observe(skip[i], 'click', EnhancedForms.skipCheck);
		}
	},
	
	addEditor: function(elementid, type) {
		enhancedEditor = true;
		
		if (type == "full") {
			var b1 = "bold,italic,formatselect,separator,bullist,numlist,link,unlink,image,separator,geneone_emoticons";
			var b2 = "indent,outdent,separator,forecolor,separator,sub,sup,separator,hr,separator,undo,redo,code";
			var b3 = "";
			var plugins = "autosave,geneone_emoticons";
		} else if (type == "blog") {
			var b1 = "bold,italic,formatselect,separator,bullist,numlist,link,unlink,image,geneone_page,separator,geneone_emoticons,separator,indent,outdent,separator,undo,redo,code";
			var b2 = "";
			var b3 = "";
			var plugins = "autosave,geneone_emoticons,geneone_page";
		} else {
			var b1 = "bold,italic,formatselect,separator,bullist,numlist,link,unlink,image,separator,geneone_emoticons,separator,indent,outdent,separator,undo,redo,code";
			var b2 = "";
			var b3 = "";
			var plugins = "autosave,geneone_emoticons";
		}

		tinyMCE.init({
			mode : "exact",
			elements : elementid,
			theme : "advanced",
			width: '100%',
			plugins : plugins,
			theme_advanced_buttons1 : b1,
			theme_advanced_buttons2 : b2,
			theme_advanced_buttons3 : b3,
			theme_advanced_blockformats : "p,h3,h4,pre,address",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			accessibility_warnings : true,
			entities : "160,nbsp,38,amp,34,quot,60,lt,62,gt,162,#162,8364,#8364,163,#163,165,#165,169,#169,174,#174,8482,#8482,8240,#8240,181,#181,183,#183,8226,#8226,8230,#8230,8242,#8242,8243,#8243,167,#167,182,#182,223,#223,8249,#8249,8250,#8250,171,#171,187,#187,8216,#8216,8217,#8217,8220,#8220,8221,#8221,8218,#8218,8222,#8222,8804,#8804,8805,#8805,8211,#8211,8212,#8212,175,#175,8254,#8254,164,#164,166,#166,168,#168,161,#161,191,#191,710,#710,732,#732,176,#176,8722,#8722,177,#177,247,#247,8260,#8260,215,#215,185,#185,178,#178,179,#179,188,#188,189,#189,190,#190,402,#402,8747,#8747,8721,#8721,8734,#8734,8730,#8730,8764,#8764,8773,#8773,8776,#8776,8800,#8800,8801,#8801,8712,#8712,8713,#8713,8715,#8715,8719,#8719,8743,#8743,8744,#8744,172,#172,8745,#8745,8746,#8746,8706,#8706,8704,#8704,8707,#8707,8709,#8709,8711,#8711,8727,#8727,8733,#8733,8736,#8736,180,#180,184,#184,170,#170,186,#186,8224,#8224,8225,#8225,192,#192,194,#194,195,#195,196,#196,197,#197,198,#198,199,#199,200,#200,202,#202,203,#203,204,#204,206,#206,207,#207,208,#208,209,#209,210,#210,212,#212,213,#213,214,#214,216,#216,338,#338,217,#217,219,#219,220,#220,376,#376,222,#222,224,#224,226,#226,227,#227,228,#228,229,#229,230,#230,231,#231,232,#232,234,#234,235,#235,236,#236,238,#238,239,#239,240,#240,241,#241,242,#242,244,#244,245,#245,246,#246,248,#248,339,#339,249,#249,251,#251,252,#252,254,#254,255,#255,914,#914,915,#915,916,#916,917,#917,918,#918,919,#919,920,#920,921,#921,922,#922,923,#923,924,#924,925,#925,926,#926,927,#927,928,#928,929,#929,931,#931,932,#932,933,#933,934,#934,935,#935,936,#936,937,#937,945,#945,946,#946,947,#947,948,#948,949,#949,950,#950,951,#951,952,#952,953,#953,954,#954,955,#955,956,#956,957,#957,958,#958,959,#959,960,#960,961,#961,962,#962,963,#963,964,#964,965,#965,966,#966,967,#967,968,#968,969,#969,8501,#8501,982,#982,8476,#8476,977,#977,978,#978,8472,#8472,8465,#8465,8592,#8592,8593,#8593,8594,#8594,8595,#8595,8596,#8596,8629,#8629,8656,#8656,8657,#8657,8658,#8658,8659,#8659,8660,#8660,8756,#8756,8834,#8834,8835,#8835,8836,#8836,8838,#8838,8839,#8839,8853,#8853,8855,#8855,8869,#8869,8901,#8901,8968,#8968,8969,#8969,8970,#8970,8971,#8971,9001,#9001,9002,#9002,9674,#9674,9824,#9824,9827,#9827,9829,#9829,9830,#9830,8194,#8194,8195,#8195,8201,#8201,8204,#8204,8205,#8205,8206,#8206,8207,#8207,173,#173,233,#233,",
			entity_encoding : "named",
			browsers : "msie,gecko,opera",
			content_css : ui['styles'] + 'editor.css',
			convert_urls : true,
			relative_urls : false
		});
		
		// Make label clickable
		var labels = document.getElementsByTagName("label");
		for (i=0;i<labels.length;i++) {
			if (labels[i].getAttribute("for") == elementid) {
				labels[i].onclick = function() { window.setTimeout("tinyMCE.execCommand(\"mceStartTyping\");tinyMCE.selectedInstance.contentWindow.focus();", 100); }
			}
		}
	},

	checkForm: function(event) {
		if (skipCheck) {
			return true;
		}
		
		if (enhancedEditor) {
			/* Copies TinyMCE content to proper form field if required */
			tinyMCE.triggerSave();
		}
		
		var form = Event.element(event);
		var fields = Form.getElements(form);
		var errors = 0;
		
		/* Check required fields are filled in */
		for (i=0;i<fields.length;i++) {
			if (Element.hasClassName(fields[i], "required") && fields[i].value == '') {
				Element.addClassName(fields[i], "missing");
				
				/* Assign an automatic ID for the element */
				if (!fields[i].id) {
					if (!this.autoId) {
						this.autoId = 1;
					}
					
					fields[i].id = 'autoid'+this.autoId;
					
					this.autoId++;
				}

				flashFormField(fields[i].id, '#F5DBD7', '#fff');
			
				errors++;
			} else {
				Element.removeClassName(fields[i], "missing");
			}
		}
		
		if (errors > 0) {
			alert('Error: '+errors+' required fields were not filled in. Please fill them in and then submit the form again.');
			Event.stop(event);
			return false;
		} else {
			return true;
		}
	},
	
	fixForms: function() {
		if (typeof(tinyMCE) != "undefined") {
			var tabEditor = function (e) {
				e = e ? e : window.event;
				if (e.keyCode == 9 && !e.shiftKey && !e.controlKey && !e.altKey) {
					var i = tinyMCE.selectedInstance;
					if(typeof i ==  'undefined') {
						return true;
					}
					
					tinyMCE.execCommand("mceStartTyping");
					this.blur();
					i.contentWindow.focus();
					e.returnValue = false;
					return false;
				}
			}
			
			if (tinyMCE.isMSIE) {
				var previousElement = document.getElementById('formobjcontent').parentNode.previousSibling.previousSibling.getElementsByTagName('input')[0];
				
				if (previousElement) {
					previousElement.onkeydown = tabEditor;
				}
			} else {
				var previousElement = document.getElementById('formobjcontent').parentNode.previousSibling.previousSibling.previousSibling.previousSibling.getElementsByTagName('input')[0];
				
				if (previousElement) {
					previousElement.onkeypress = tabEditor;
				}
			}
		}
	},
	
	skipCheck: function() {
		skipCheck = true;
	}
}

Event.observe(window, 'load', EnhancedForms.addCheck, false);
Event.observe(window, 'load', EnhancedForms.fixForms, false);